package com.weyoung.admin.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weyoung.admin.dto.req.AdminFamilyAddDTO;
import com.weyoung.admin.dto.req.FamilyAddDTO;
import com.weyoung.admin.mapper.UserMapper;
import com.weyoung.admin.service.FamilyService;
import com.weyoung.admin.mapper.FamilyMapper;
import com.weyoung.admin.service.UserService;
import com.weyoung.common.model.entity.FamilyDO;
import com.weyoung.common.model.entity.UserDO;
import com.weyoung.common.model.entity.UserDetailDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateTime.now;

/**
* @author 86182
* @description 针对表【family(家族信息表)】的数据库操作Service实现
* @createDate 2025-04-14 16:37:25
*/
@Service
@Slf4j
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, FamilyDO>
    implements FamilyService{

    @Autowired
    private FamilyMapper familyMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public List<FamilyDO> getByNmae(String name) {
        List<FamilyDO> familyDO = familyMapper.selectByName(name);
        return familyDO;
    }

    @Override
    public void add(FamilyAddDTO familyAddDTO) {
        //获取当前操作用用户的id（如果是创建家族就可以直接用来做创始人）
        Integer user_id = Integer.parseInt(StpUtil.getLoginId().toString());

        String family_id = userMapper.selectById(user_id).getFamilyId();
        FamilyDO familyDO = familyMapper.selectById(family_id);
        //根据家族有无存在决定是新增还是修改1
        if(familyDO==null || familyDO.getCreatorId() != user_id){
            //如果没有记录过，就新增
            FamilyDO family = BeanUtil.copyProperties(familyAddDTO, FamilyDO.class);
            Date now = now();
            family.setUpdateDate(now);
            family.setCreateDate(now);
            family.setCreatorId(user_id);
            familyMapper.insert(family);
        }
        else {
            //存在记录，就改动
            Date now = now();
            UpdateWrapper<FamilyDO> updateWrapper = new UpdateWrapper<>();
            updateWrapper.lambda().eq(FamilyDO::getFamilyId, family_id)
                    .set(FamilyDO::getFamilyName, familyAddDTO.getFamilyName())
                    .set(FamilyDO::getFamilyMotto, familyAddDTO.getFamilyMotto())
                    .set(FamilyDO::getHeritage, familyDO.getHeritage())
                    .set(FamilyDO::getUpdateDate, now);

            update(updateWrapper);
        }
    }

    @Override
    public void add1(AdminFamilyAddDTO familyAddDTO) {
        FamilyDO familyDO = familyMapper.selectById(familyAddDTO.getFamilyId());
        UserDO userDO = userMapper.selectById(familyAddDTO.getCreatorId());

        if(familyAddDTO.getCreatorId() != null && familyAddDTO.getFamilyId() == null){
            throw new IllegalArgumentException("输入的用户id不存在");
        }

        if(familyAddDTO.getFamilyId() == null && familyAddDTO.getCreatorId() == null){
            throw new IllegalArgumentException("家族id与家族管理员id不能同时为空");
        }
        else if(familyDO == null && userDO == null){
            throw new IllegalArgumentException("家族id无效且家族管理id为空，操作无效");
        }
        else{
            if((familyDO == null || familyAddDTO.getFamilyId() == null) && userDO != null){
                //如果家族不存在，但是传入了用户id
                if(userDO.getFamilyId() != null) throw new IllegalArgumentException("用户已有家族，无法新建家族");
                else {
                    //如果该用户没有家族，则新建家族，并设置家族管理员为该用户
                    FamilyDO family = BeanUtil.copyProperties(familyAddDTO, FamilyDO.class);
                    Date now = now();
                    family.setUpdateDate(now);
                    family.setCreateDate(now);
                    familyMapper.insert(family);
                }
            }
            else if((userDO == null || familyAddDTO.getCreatorId() == null) && familyDO != null){
                //修改家族信息
                Date now = now();
                UpdateWrapper<FamilyDO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(FamilyDO::getFamilyId, familyAddDTO.getFamilyId())
                        .set(FamilyDO::getFamilyName, familyAddDTO.getFamilyName())
                        .set(FamilyDO::getFamilyMotto, familyAddDTO.getFamilyMotto())
                        .set(FamilyDO::getHeritage, familyDO.getHeritage())
                        .set(FamilyDO::getUpdateDate, now);
                update(updateWrapper);
            }
            else if(userDO != null && familyDO != null){
                //如果家族存在，并且传入了有效用户id     (修改家族的同时，将其设置为家族创建者)
                Date now = now();
                UpdateWrapper<FamilyDO> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(FamilyDO::getFamilyId, familyAddDTO.getFamilyId())
                        .set(FamilyDO::getFamilyName, familyAddDTO.getFamilyName())
                        .set(FamilyDO::getFamilyMotto, familyAddDTO.getFamilyMotto())
                        .set(FamilyDO::getHeritage, familyDO.getHeritage())
                        .set(FamilyDO::getUpdateDate, now)
                        .set(FamilyDO::getCreatorId, familyAddDTO.getCreatorId());
                update(updateWrapper);
            }

        }


    }

    @Override
    public void delete(String id) {
        FamilyDO familyDO =  familyMapper.selectById(id);
        if(familyDO==null){
            log.info("需要删除的家族不存在！");
            return;
        }
        familyMapper.delete1(id);


    }
}




